module.exports = class extends think.Controller {
    __before() {
        // console.log('12312')
    }

    __after() {
        // console.log('222222')
    }

    __call() {
        this.body = {
            code: 1000,
            data: '非法操作'
        }
    }

    async indexAction() {
        let model1 = this.model("user");
        let data = await model1.where({username: "张三"}).find();
        this.assign('password', data.password);
        return this.display();
    }

    async addAction() {
        let result = await this.model('user').add({username: '王五', password: 'jkljkl'});
        this.success(result);
    }

    async delAction() {
        let result = await this.model('user').where({username: '王五'}).delete();
        this.success(result);
    }

    async updateAction() {
        let result = await this.model('user').where({username: '张三'}).update({password: 'thinkjs'});
    }

    a//修改密码
    
    async changePasswordAction() {
        let username = this.post('username')
        let password = think.md5(this.post('password'))
        
        if (this.isPost) {
            let data = await this.model('user').where({username: username}).find()
            if (think.isEmpty(data)) {
                this.body = {
                    code: '2000',
                    msg: '手机帐号不正确'
                }
            } else {
                await this.model('user').where({username: username}).update({password: password})
                this.body = {
                    code: '0000',
                    msg: '修改密码成功'
                }
            }
        } else {
            this.body = {
                code: '1000',
                msg: '非法操作'
            }
        }
    }
    
    //登录
    async loginAction() {
        
        let username = this.post('username')
        let password = think.md5(this.post('password'))
        
        if (this.isPost) {
            
            let data = await this.model('user').where({username: username, password: password}).find()
            console.log(data)
            if (think.isEmpty(data)) {
                
                this.body = {
                    code: '2000',
                    msg: '帐号或者密码不正确'
                }
                
            } else {
                this.body = {
                    code: '0000',
                    msg: '登录成功'
                }
            }
        } else {
            this.body = {
                code: '1000',
                msg: '非法操作'
            }
        }
    }
    
    //注册
  async registerAction() {
        
        let time = new Date().valueOf() / 1000
        
        let username = this.post('username')
        let password = think.md5(this.post('password'))
        let session = think.md5(this.post('username'))
        
        if (this.isPost) {
            
            let data = await this.model('user').where({username: username}).find()
            
            if (think.isEmpty(data)) {
                
                await this.session('session_key', session);
                
                let msg = await this.model('user').add({
                    username: username,
                    password: password,
                    add_time: time,
                    session_key: await this.session('session_key')
                })
                
                this.body = {
                    code: '0000',
                    msg: '注册成功'
                }
                
            } else {
                this.body = {
                    code: '2000',
                    msg: '手机号/帐号已被注册'
                }
            }
        } else {
            this.body = {
                code: '1000',
                msg: '非法操作'
            }
        }
    }
}